#include "TDropdown.h"

#include "../DevInclude.h"
#include "../utils/Utils.h"



TDropdown::TDropdown(){
    
     m_pOverListener=NULL;
     m_pOverfnSelector=NULL;
}
TDropdown::~TDropdown(){
    if(getOverPanel()){
        getOverPanel()->release();
    }
}

bool TDropdown::init(){
     if(!TPanel::init()){
         return false;
     }
     
     
     return true;
}

TDropdown*  TDropdown::create(xml_node<> * pItem){
    
    TDropdown*  ret=TDropdown::create();
    ret->initWidthConf(pItem);
    return ret;
}

void  TDropdown::initWidthConf(xml_node<> * pItem){
    TPanel::initWidthConf(pItem);
}


void TDropdown::onCreateComplete(){
    if(getOverPanel()){
        getOverPanel()->retain();
    }
    
    onHide();
    getOverPanel()->setTarget(this, menu_selector(TDropdown::onOverSelected));
}
 

TPanel* TDropdown::getSelectPanel(){
    return  (TPanel*)getUI("selectItem");
}
TPanel* TDropdown::getOverPanel(){
    return  (TPanel*)getUI("overPanel");
}

void TDropdown::onShow(){
    if(!getOverPanel() ){
        return;
    }
    
    if(getOverPanel() && getOverPanel()->getParent()==NULL){
        CCDirector::sharedDirector()->getRunningScene()->addChild(getOverPanel(),100);
    } 
    
    CCPoint containerPos=  getPosInScreen(CCPointZero, this);
  
  
    getOverPanel()->setPositionY(containerPos.y-getContentSize().height);
    getOverPanel()->setPositionX(containerPos.x-getOverPanel()->getContentSize().width/2+getContentSize().width/2);
    
    
}

void TDropdown::setTarget(CCObject *rec, SEL_MenuHandler selector){
    m_pOverListener=rec;
    m_pOverfnSelector=selector;
}

void TDropdown::onOverSelected(CCObject* target){
    
    if(m_pOverListener&& m_pOverfnSelector)
        (m_pOverListener->*m_pOverfnSelector)(target);
}
void TDropdown::procTuiEvent(const string& event,TPanelItem*){
    if(event=="showDrop"){
        onShow();
    }
}

void TDropdown::onHide(){
    if(getOverPanel() && getOverPanel() ->getParent()){
        getOverPanel() ->getParent()->removeChild(getOverPanel() , true);
    }
}
 
 